package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 负责主页面的新增，详情，编辑，删除
 */

@Controller
@RequestMapping(value = "/movie")
public class MovieController {

    @Autowired
    private MovieRepository movieRepository;

    //电影主页
    @RequestMapping(value = "/index")
    public String index(Model model) {
        List<Movie> list = movieRepository.findAll();
        model.addAttribute("list", list);
        return "movie/index";
    }

    //电影新增页面
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toAdd() {
        return "movie/add";
    }

    /**
     * 执行保存操作，然后重定向到列表上面index.html
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@RequestParam String name,
                        @RequestParam String photo,
                        @RequestParam String createDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }

        System.out.println("name=" + name);
        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        movieRepository.save(movie);
        //TODO:执行保存操作
        //重定向到index.html页面
        //有两种方式：重定向（浏览器行为），请求转发（服务器端完成）
        //redirect: 跟浏览器说明是重定向
        return "redirect:/movie/index";
    }

    //电影详情页面
    @RequestMapping("/detail/{id}")
    public String detail(Model model, @PathVariable Long id) {
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info", movie);
        return "movie/detail";
    }


    //电影编辑页面
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id) {
        //获取数据
        Movie movie = movieRepository.findOne(id);
        //把数据添加到movie对象，提交给视图层Thyealeaf
        model.addAttribute("info", movie);
        //跳转到视图页面edit.html
        return "movie/edit";
    }

    /**
     * 执行更新操作
     *
     * @param id
     * @param name
     * @param photo
     * @param createDate
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(@RequestParam Long id,
                         @RequestParam String name,
                         @RequestParam String photo,
                         @RequestParam String createDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }
        //1.通过id获取数据库的movie对象
        Movie movie = movieRepository.findOne(id);
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);

        //通过调用数据访问接口保存数据
        //根据ID是否为NULL来判断是否更新或者修改
        movieRepository.save(movie);

        //重定向到index.html页面
        return "redirect:/movie/index";
    }

    /**
     * 通过id删除movie对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable Long id) {
        //使用数据访问接口删除方法
        movieRepository.delete(id);
        //重定向到index.html页面
        return "redirect:/movie/index";
    }
}
